<template>
	<view>
		<view class="goods-list">
            <view @click="gotoDetail(goods)" v-for="(goods,index) in goodlist" :key="index">
                <my-goods :goods="goods"></my-goods>
            </view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
                // 请求参数对象
                queryObj: {
                    query: '', //查询关键字
                    cid: '', //商品分类id
                    pagenum: 1, //页码值
                    pagesize: 10, //每页显示多少条数据
                },
                
                goodlist: [], //商品列表数据
                total: 0, //总数量,用来实现分页
                isloading: false, // 节流阀
			};
		},
        
        methods: {
            async getGoodsList(cb) {
                try{
                    // 打开节流阀
                    this.isloading = true
                    const {data: res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
                    this.goodlist = [...this.goodlist,...res.message.goods]
                    this.total = res.message.total
                    this.isloading = false
                    cb && cb()
                }catch(e){
                    uni.$showMsg()
                }
            },
            
            // 点击item项跳转到商品详情页面
            gotoDetail(goods) {
                uni.navigateTo({
                    url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
                })
            }
        },
        
        onLoad(options) {
            this.queryObj.query = options.query || ''
            this.queryObj.cid = options.cid || ''
            this.getGoodsList()
        },
        
        // 上啦触底事件
        onReachBottom() {
            // 让页码值自增+1
            if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('没有数据了')
            if(this.isloading) return
            this.queryObj.pagenum++
            this.getGoodsList()
        },
        
        //下啦触底事件
        onPullDownRefresh() {
            // 重置关键数据
            this.queryObj.pagenum = 1
            this.total = 0
            this.isloading = false
            this.goodlist = []
            
            // 重新发起请求
            this.getGoodsList(() => uni.stopPullDownRefresh())
        }
	}
</script>

<style lang="scss">
    
</style>
